<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="14">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="banner图片" :required="true">
            <el-upload
              v-loading="loading1"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :http-request="upLoadServe"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input
              oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
              maxlength="3"
              v-model="form.sort"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              class="tablescope"
              v-model="form.status"
              :active-value="1"
              :inactive-value="0"
              active-text="禁用"
              inactive-text="启用"
              inactive-color=""
            >
            </el-switch>
            <!-- <el-switch
              class="tablescope"
              v-model="form.status"
              :active-value="1"
              :inactive-value="0"
              inactive-color=""
            >
            </el-switch> -->
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { upLoad } from "@/api/index";
import { doBanner } from "@/api/shopSystem";
import upImg from "@/utils/upimg";
export default {
  props: ["rowData"],
  data() {
    return {
      loading1: false,
      form: {
        title: "",
        position: "",
        shopId: "",
        sort: "",
        status: 1,
      },
      imageUrl: "",
      imageUrlId: "",
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
    };
  },
  methods: {
    async doBanner(params) {
      const { err, msg } = await doBanner({
        id: this.rowData.id,
        title: this.form.title,
        img: this.imageUrlId,
        status: this.form.status,
        sort: this.form.sort,
        action: params,
      });
      if (err === 0) {
        this.$emit("closeDialog");
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //上传图片
    async upLoad(formData) {
      const { data, err } = await upLoad(formData);
      if (err === 0) {
        this.imageUrl = data.file_src;
        this.imageUrlId = data.file_id;
      }
    },
    upLoadServe(file) {
      this.loading1 = true;
      upImg.upimg(
        (data) => {
          this.imageUrl = data.data.pre_img;
          this.imageUrlId = data.data.url;
          this.loading1 = false;
        },
        1,
        file
      );
      //   let formData = new FormData();
      //   formData.append("file", file.file);
      //   this.upLoad(formData);
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.rowData.id) {
            //编辑
            let MOD = "MOD";
            this.doBanner(MOD);
          } else {
            //新增
            let ADD = "ADD";
            this.doBanner(ADD);
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {
    console.log(this.rowData);
    if (this.rowData.id) {
      this.form.title = this.rowData.title;
      this.imageUrl = this.rowData.img.src;
      this.imageUrlId = this.rowData.img.src;
      this.form.sort = this.rowData.sort;
      this.form.status = this.rowData.status;
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.el-breadcrumb__inner {
  cursor: pointer !important;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
/deep/.el-upload--picture-card {
  width: 100px;
  height: 100px;
  line-height: 100px;
}
/deep/.el-upload-list__item {
  width: 100px !important;
  height: 100px !important;
  border: 0 !important;
}
.avatar-uploader {
  width: 80px;
  height: 80px;
}
</style>
